package com.vanrin.aweatherforecast.view;

import java.util.ArrayList;
import java.util.Iterator;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.SurfaceHolder;

public class dwList {

    private Paint paint = null;

    private long draw_time;

    private ArrayList<dwPoint> poList;

    public void begin_record()

    {

        dwPoint.initial_time = System.currentTimeMillis();

        poList.clear();

    }

    public void record(int x, int y, int ty, int color)

    {
        dwPoint po = new dwPoint(x, y, ty, color);

        poList.add(po);

    }

    public dwList() {

        paint = new Paint();

        poList = new ArrayList<dwPoint>();

    }

    public void draw(SurfaceHolder holder) {

        dwPoint po;

        long curr_time;

        long base_time = 0;

        int nnn = 0;

        Iterator<dwPoint> it = poList.iterator();

        while (it.hasNext()) {

            po = it.next();

            draw_time = po.m_timeSpan * 100;

            if (nnn == 0)

                base_time = System.currentTimeMillis();

            nnn++;

            // --------- waiting ----------------------------------

            do {

                curr_time = System.currentTimeMillis() - base_time;

            }

            while (curr_time < draw_time);

            // ------------------------------------------------------

            paint(holder, nnn);

        }

    }

    public void paint(SurfaceHolder holder, int k) {

        Canvas canvas = holder.lockCanvas();

        dwPoint po;

        int lastX = 0;

        int lastY = 0;

        Iterator<dwPoint> it = poList.iterator();

        for (int i = 0; i < k; i++) {

            po = it.next();

            if (po.m_type == 0) {

                lastX = po.m_x;

                lastY = po.m_y;

            }

            else {

                paint.setColor(po.m_color);

                paint.setStrokeWidth(3);

                canvas.drawLine(lastX, lastY, po.m_x, po.m_y, paint);

                lastX = po.m_x;

                lastY = po.m_y;

            }

        }

        holder.unlockCanvasAndPost(canvas);

    }

}